// pages/details/details.js
const { getGoodsDetail } = require("../../api/Home");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: false,
    num: 0,
    imgs: [
    ],
    goods: {
      id: 2,
      goodsImage: "/images/banner01.jpg",
      goodsName: "商品名称",
      goodsImgs: [
        "/images/banner01.jpg",
        "/images/banner02.jpg",
        "/images/banner03.jpg",
        "/images/banner04.jpg",
      ],
      goodsAddress: "广州",
      goodsPrice: "200",
      goodsPriceOld: "300",
      goodsDetails: "/images/IMG_0466.jpg"

    }
  },
  pushcar() {
    wx.reLaunch({
      url: '/pages/car/car'
    })
  },
  pushhome() {
    wx.reLaunch({
      url: '/pages/home/home'
    })
  },
  collect() {
    this.data.flag = !this.data.flag
    this.setData({
      flag: this.data.flag
    })
    let cartLists = {
      name: this.data.goods.goodsName,
      price: this.data.goods.goodsPrice,
      url: this.data.goods.goodsImage,
      id: this.data.goods.id
    };
    let collectionList = wx.getStorageSync("collectionList");
    console.log(collectionList);
    if (this.data.flag == true) {
      collectionList.push(cartLists)
    } else {
      collectionList.forEach((item, index) => {
        collectionList.splice(index, 1)
      });
    }
    wx.setStorageSync("collectionList", collectionList)
  },
  addTo() {
    // 购物车数据结构
    let cartLists = {
      id: this.data.goods.id,
      goodsImage: this.data.goods.goodsImage,
      goodsName: this.data.goods.goodsName,
      goodsPrice: this.data.goods.goodsPrice,
      num: 1
    };
    let goodsCarList = wx.getStorageSync("goodsCarList");
    if (goodsCarList) {
      let index = goodsCarList.findIndex(item => item.id == this.data.goods.id);
      if (index != -1) {
        goodsCarList[index].num += 1;
      } else {
        goodsCarList.push(cartLists);
      }
      wx.setStorageSync('goodsCarList', goodsCarList)
    } else {
      wx.setStorageSync('goodsCarList', [cartLists]);
    }
    // 更新商品数量
    this.setData({
      num: wx.getStorageSync('goodsCarList').length
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getGoodsDetail({ goods_id: options.id }).then((data) => {
      let { goods_id, goods_small_logo, goods_name, pics, goods_price, goods_introduce } = data.message;
      let goods = {
        id: goods_id,
        goodsImage: goods_small_logo,
        goodsName: goods_name,
        goodsImgs: pics,
        goodsAddress: "广州",
        goodsPrice: goods_price,
        goodsPriceOld: goods_price + 100,
        goodsDetails: goods_introduce
      }
      this.setData({
        goods
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      num: wx.getStorageSync('goodsCarList').length
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})